package com.briup.cms.service.impl;

import com.briup.cms.bean.Category;
import com.briup.cms.dao.CategoryDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.CategoryService;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    //分页获取所有分类
    @Override
    public Page<Category> findAll(Integer pageNum, Integer pageSize) throws ServiceException {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        return categoryDao.findAll(pageRequest);
    }

    //新增分类或者更新分类
    @Override
    public void saveOrUpdateCategory(Category category) throws ServiceException {
        //非空
        if (category.getName()==null||category.getName().trim().equals("")){
            throw  new ServiceException(ResultCode.CATEGORY_NAME_ISNULL);
        }
        //唯一
        Category category1 = findCategoryByUsername(category.getName());
        if (category1!=null){
            category1.setDescription(category.getDescription());
            categoryDao.save(category1);
        }else {
            categoryDao.save(category);
        }
    }

    //批量删除分类
    @Override
    public void deleteCategoryInBatch(List<Long> ids) throws ServiceException {
        for (Long id:ids){
            categoryDao.deleteById(id);
        }
    }

    @Override
    public void deleteCategoryById(Long id) throws ServiceException {
        Category category = findCategoryById(id);
        if (category==null){
            throw new ServiceException(ResultCode.CATEGORY_NOT_EXIST);
        }
        categoryDao.deleteById(id);
    }

    //按序号升序查询分类信息，并进行分页
    @Override
    public Page<Category> findAllSortbyno(Integer pageNum, Integer pageSize) throws ServiceException {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.ASC,"no"));
        return categoryDao.findAll(pageRequest);
    }

    //更新分类序号
    @Override
    public void updateCategoryNo(Long id, int no) throws ServiceException {
        Category category =findCategoryById(id);
        if (category==null){
            throw new ServiceException(ResultCode.CATEGORY_NOT_EXIST);
        }
        category.setNo(no);
        categoryDao.save(category);
    }

    @Override
    public Category findCategoryByUsername(String name) throws ServiceException {
        //非空
        if (name==null||name.trim().equals("")){
            throw  new ServiceException(ResultCode.CATEGORY_NAME_ISNULL);
        }

        return categoryDao.findCategoryByName(name);
    }

    @Override
    public Category findCategoryById(Long id) throws ServiceException {
        return categoryDao.findById(id).orElse(null);
    }
}
